package edu.caltech.android;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.OverlayItem;

/**
 * Extend OverlayItem to add quake-specific fields TODO: scale the displayed
 * icon to reflect quake magnitude, or choose a different icon (e.g. color)
 * based on magnitude
 * 
 * @author mfaulk
 * 
 */
public class QuakeOverlayItem extends OverlayItem {
	private final float magnitude;
	private final long timeMillis;
	private boolean recent;
	private double speed;
	private double period;
	private long tapTime;
	/**
	 * 
	 * @param point
	 * @param title
	 * @param snippet
	 * @param magnitude
	 * @param latitude
	 * @param longitude
	 */
	public QuakeOverlayItem(GeoPoint point, String title, String snippet,
			float magnitude, long timeMillis) {
		super(point, title, snippet);
		this.magnitude = magnitude;
		this.timeMillis = timeMillis;
		this.recent = true;
		this.speed = 6000.0;
		this.period = 2.0;
		
		// decide whether the earthquake has reached its max range of effect with a rough approximation
		// top speed of a surface wave is around 8 km/s
		
		this.recent = (speed * getAge() <  maxQuakeRange());
		
		// TODO: set marker color
	}
	
	/*
	 * Returns the approximate maximum range of an earthquake with given magnitude
	 * in meters.
	 */
	private double maxQuakeRange() {
		switch (Math.round(magnitude)) {
			case 1: return 10.0;
			case 2: return 50.0;
			case 3: return 100.0;
			case 4: return 5000.0;
			case 5: return 10000.0;
			case 6: return 20000.0;
			case 7: return 50000.0;
			default:
					return 100000.0;
		}
	}
	
	/*
	 * Returns the magnitude of the earthquake.
	 */
	public float getMagnitude() {
		return magnitude;
	}
	
	/*
	 * Returns the age of the time of occurrence of the earthquake
	 * Note: returns time in milliseconds from midnight January 1st, 1970.
	 */
	public long getTimeMillis() {
		return timeMillis;
	}
	
	/*
	 * Returns the age of the earthquake relative to now, in seconds.
	 */
	public double getAge() {
		return (System.currentTimeMillis() - timeMillis) / 1000.0;
	}
	
	/*
	 * Returns the speed of the earthquake.
	 */
	public double getSpeed() {
		return speed;
	}
	
	/*
	 *	Returns the approximated maximum range of the earthquake (in meters)
	 */
	public float getRadius(){
		recent = (speed * getAge() <  maxQuakeRange());
		if (recent)
			return (float) (getAge() * speed);
		else
			return (float) maxQuakeRange();
	}
	
	/*
	 *	Returns the period of the surface waves;
	 */
	public double getPeriod() {
		return period;
	}
	
	/*
	 *	Mutator for the time when the overlay was clicked.
	 */
	public void setTapTime(long value) {
		this.tapTime = value;
	}
	
	/*
	 *	Accessor for the time when the overlay was clicked.
	 */
	public long getTapTime() {
		return tapTime;
	}
	
}
